package com.xingkeduo.business.operation.controller;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.operation.domain.BoardDomain;
import com.xingkeduo.pojo.json.BoardJson;
import com.xingkeduo.pojo.query.BoardQuery;
import com.xingkeduo.pojo.table.BoardTable;
import com.xingkeduo.util.pojo.domain.Page;
import com.xingkeduo.util.tool.ReturnUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import static com.xingkeduo.tool.OperaConstants.CMS_URL_PREFIX;


/**
 * 模块管理
 *
 * @author wangxilong <wangxilong@xingkeduo.com>
 * @version id:v0.1 ${date}${time}
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@RestController
@RequestMapping(CMS_URL_PREFIX + "/board")
public class BoardController extends InitController {

    @Autowired
    private BoardDomain boardDomain;

    //查看
    @RequestMapping(value = "/{urlParam:.+}", method = RequestMethod.GET)
    public String doGet(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            BoardQuery boardQuery = BoardJson.createQueryForBoard(urlParam);
            long code = boardQuery.getCode();
            BoardTable boardTable = boardDomain.getBoardByCode(code);
            return returnUtil.getSuccess(BoardJson.toJson(boardTable));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //添加
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String doPost(@RequestBody @Valid BoardTable boardVo, ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
           // this.errMsg(bindingResult);
            boardVo.setCreatedBy(this.getOperator(request));
            boardDomain.createBoard(boardVo);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //修改
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.PUT)
    public String doPut(@PathVariable String urlParam, @RequestBody @Valid BoardTable boardVo,ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            BoardTable boardTable = BoardJson.createTableForModifyBoard(urlParam,boardVo);
            boardTable.setUpdatedBy(this.getOperator(request));
            boardDomain.modifyBoard(boardTable);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //删除
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.DELETE)
    public String doDelete(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            BoardQuery boardQuery = BoardJson.createQueryForBoard(urlParam);
            long id = boardQuery.getId();
            boardDomain.delBoard(id);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //查询列表
    @RequestMapping(value = "/lists/{urlParam}", method = RequestMethod.GET)
    public String doGetLists(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            BoardQuery boardQuery = JSON.parseObject(urlParam,BoardQuery.class);

            Page<BoardTable> countryList= boardDomain.getBoardList(boardQuery,boardQuery.getPageNum());

            return returnUtil.getSuccess(BoardJson.toJson(countryList));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }
}
